//Copyright (c) 2007 Andrew Badera andrew@badera.us
//SVN repository at http://powncenet.googlecode.com/svn/

//Permission is hereby granted, free of charge, to any person
//obtaining a copy of this software and associated documentation
//files (the "Software"), to deal in the Software without
//restriction, including without limitation the rights to use,
//copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the
//Software is furnished to do so, subject to the following
//conditions:

//The above copyright notice and this permission notice shall be
//included in all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//OTHER DEALINGS IN THE SOFTWARE.


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace BadException
{
    public static class BadLogger
    {
        public static bool _doEventViewer = true;
        public static bool _doLogFile = true;

        public static string CLIENT_APP = "PownceNET 0.1";

        private static readonly EventLog _eventLog = new EventLog();

        public static void WriteToLog(string source, string message, ExceptionEnums.LogEntryType entryType)
        {
            _eventLog.Source = BadLogger.CLIENT_APP;
            string locmessage = string.Concat(new object[] {"Source: ", source, Environment.NewLine, "Message: ", Environment.NewLine});
            
            if (_doEventViewer)
            {
                switch (entryType)
                {
                    case ExceptionEnums.LogEntryType.Error:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.Error);
                        break;
                    case ExceptionEnums.LogEntryType.FailAudit:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.FailureAudit);
                        break;
                    case ExceptionEnums.LogEntryType.Info:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.Information);
                        break;
                    case ExceptionEnums.LogEntryType.SuccessAudit:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.SuccessAudit);
                        break;
                    case ExceptionEnums.LogEntryType.Unknown:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.Error);
                        break;
                    case ExceptionEnums.LogEntryType.Warning:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.Warning);
                        break;
 
                    default:
                        _eventLog.WriteEntry(locmessage, EventLogEntryType.Error);
                        break;
                }
            }

            if (_doLogFile)
            {
                string logmessage = string.Concat(@"/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\", Environment.NewLine, "Date/Time: ", DateTime.Now.TimeOfDay.ToString(), Environment.NewLine, locmessage, Environment.NewLine);

                string logFile = string.Concat(Environment.CurrentDirectory, "\\", "BadLog.log");

                using (StreamWriter sw = new StreamWriter(logFile, true))
                {
                    sw.WriteLine(logmessage);
                }

            }
        }

        public static void WriteToLog(string source, Exception ex, ExceptionEnums.LogEntryType entryType)
        {
            WriteToLog(source, string.Concat(new object[] {"Type: ", ex.GetType().ToString(), Environment.NewLine, "Message: ", ex.Message, Environment.NewLine, "Trace: ", ex.StackTrace, Environment.NewLine}), entryType);
        }
    }
}
